#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

namespace Utility.Attributes {
    [CustomPropertyDrawer(typeof(NotNullAttribute))]
    public class NotNullDrawer : PropertyDrawer {
        private static GUIStyle m_errorHelpBoxStyle;

        private static GUIStyle ErrorHelpBoxStyle {
            get {
                if (m_errorHelpBoxStyle == null) {
                    m_errorHelpBoxStyle = new GUIStyle(EditorStyles.helpBox); 

                    Texture2D backgroundTexture = new Texture2D(1, 1);
                    backgroundTexture.SetPixel(0, 0, new Color(1.0f, 1.0f, 0.8f, 1.0f));
                    backgroundTexture.Apply();

                    m_errorHelpBoxStyle.normal.background = backgroundTexture;
                    m_errorHelpBoxStyle.normal.textColor = Color.red;
                }
                return m_errorHelpBoxStyle;
            }
        }

        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
            EditorGUI.PropertyField(position, property, label);
            if (!property.objectReferenceValue) {
                EditorGUI.LabelField(position, label.text, "Reference is missing!");
            }
        }
    }
}
#endif
